/**
 * 函数节流
 * @param {*} fn 
 * @param {*} wait 
 * @returns 
 */
const throttleXDC = (fn, wait) => {
    var context, args, timeout, result
    var previous = 0
    var later = function () {
        previous = new Date()
        timeout = null
        result = fn.apply(context, args)
    }
    return function () {
        context = this
        args = arguments
        var now = new Date()
        var remaining = wait - (now - previous)
        if (remaining <= 0) {
            clearTimeout(timeout)
            timeout = null
            previous = now
            result = fn.apply(context, args)
        } else if (!timeout) {
            timeout = setTimeout(later, remaining)
        }
        return result
    }
}

export {
    throttleXDC
}